' the object's DoneOnThisScanline flag appropriately.
Public Sub RayTraceable_CullScanline(ByVal px As Single, ByVal py As Single, ByVal pz As Single, ByVal Nx As Single, ByVal Ny As Single, ByVal Nz As Single)
Dim dx As Single
Dim dy As Single
Dim dz As Single
Dim dist As Single
' See if we will ever be visible again.
If ForeverCulled Then
DoneOnThisScanline = True
Exit Sub
End If
' We have not yet had a hit on this scanline.
HadHit = False
' Find the distance from the center of the
' sphere to the scanline plane.
' Get the vector from our center to the point.
With Center
dx = .Trans(1) - px
dy = .Trans(2) - py
dz = .Trans(3) - pz
End With
' Take the dot product of this and the normal.
' If the resulting distance > Radius, cull.
DoneOnThisScanline = (Abs(dx * Nx + dy * Ny + dz * Nz) > Radius)
' See if we will be culled in the future.
If DoneOnThisScanline Then
' We were not culled on a previous scanline
' but we are now. We will be culled on
' all later scanlines.
If HadHitOnPreviousScanline Then ForeverCulled = True
Else
' We are not culled. Remember that.
HadHitOnPreviousScanline = True
End If
End Sub
' Return the value T for the point of intersection
' between the vector from point (px, py, pz) in
' the direction <vx, vy, vz>.
'
' direct_calculation is true if we are finding the
' intersection from a viewing position ray. It is
' false if we are finding an reflected intersection
' or a shadow feeler.
Public Function RayTraceable_FindT(ByVal direct_calculation As Boolean, ByVal px As Single, ByVal py As Single, ByVal pz As Single, ByVal Vx As Single, ByVal Vy As Single, ByVal Vz As Single) As Single
Dim A As Single
Dim B As Single
Dim C As Single
Dim Cx As Single
Dim Cy As Single
Dim Cz As Single
Dim B24AC As Single
Dim t1 As Single
Dim t2 As Single
Dim dx As Single
Dim dy As Single
Dim dz As Single
' See if we have been culled.
If direct_calculation And DoneOnThisScanline Then
RayTraceable_FindT = -1
Exit Function
End If
Cx = Center.Trans(1)
Cy = Center.Trans(2)
Cz = Center.Trans(3)
' Get the coefficients for the quadratic.
A = Vx * Vx + Vy * Vy + Vz * Vz
B = 2 * Vx * (px - Cx) + _
2 * Vy * (py - Cy) + _
2 * Vz * (pz - Cz)
C = Cx * Cx + Cy * Cy + Cz * Cz + _
px * px + py * py + pz * pz - _
2 * (Cx * px + Cy * py + Cz * pz) - _
Radius * Radius
' Solve the quadratic A*t^2 + B*t + C = 0.
B24AC = B * B - 4 * A * C
If B24AC < 0 Then
' There is no real intersection.
RayTraceable_FindT = -1
' If we had a hit before on this scanline
' but we don't have one now. We are done
' for this scanline.
If HadHit And direct_calculation Then DoneOnThisScanline = True
Exit Function
ElseIf B24AC = 0 Then
' There is one intersection.
t1 = -B / 2 / A
Else
' There are two intersections.
B24AC = Sqr(B24AC)
t1 = (-B + B24AC) / 2 / A
t2 = (-B - B24AC) / 2 / A
' Use only positive t values.
If t1 < 0.01 Then t1 = t2
If t2 < 0.01 Then t2 = t1
' Use the smaller t value.
If t1 > t2 Then t1 = t2
End If
' If there is no positive t value, there's no
' intersection in this direction.
If t1 < 0.01 Then
RayTraceable_FindT = -1
' If we had a hit before on this scanline
' but we don't have one now. We are done
' for this scanline.
If HadHit And direct_calculation Then DoneOnThisScanline = True
Exit Function
End If
' We had a hit.
If direct_calculation Then HadHit = True
RayTraceable_FindT = t1
End Function
' Return the minimum and maximum distances from
' this point.
Private Sub RayTraceable_GetRminRmax(new_min As Single, new_max As Single, ByVal X As Single, ByVal Y As Single, ByVal Z As Single)